package com.zj.leetcode;

import java.util.Arrays;

public class A88合并两个有序数组 {
    public static void main(String[] args) {
        int m=3,n=3;
        int[] nums1 = {1,2,3,0,0,0};
        int[] nums2 = {2,5,6};
        merge2(nums1,m,nums2,n);

    }


    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        if(n==0){
            return;
        }
        int[] numstemp = new int[m+n];
        for (int i = 0, j = 0; i  +j<m+n; ) {
            if(j>=n||(nums1[i] > 0&&nums1[i] < nums2[j])){
                numstemp[i +j] = nums1[i];
                i++;
            }else if(nums1[i] == 0||nums1[i] >=nums2[j]) {
                numstemp[i +j] = nums2[j];
                j++;
            }
        }
        System.out.println(Arrays.toString(numstemp));
        for (int i = 0; i < m+n; i++) {
            nums1[i]=numstemp[i];
        }
    }

    public static void merge2(int[] nums1, int m, int[] nums2, int n) {
        int idx=m+n-1;
        for (int i = m-1, j = n-1; j>=0; idx--) {
            if(i==-1||nums1[i] < nums2[j]){
                nums1[idx] = nums2[j];
                j--;
            }else if(nums1[i] >=nums2[j]) {
                nums1[idx] = nums1[i];
                i--;
            }
        }
        System.out.println(Arrays.toString(nums1));

    }
}
